// pages/classifyList/classifyList.js
var app=getApp();
var util=require("../../utils/util");
var home=require("../../api/home");


Page({
  /* 页面的初始数据*/
  data: {
    ids: 0,
    width: wx.getSystemInfoSync().windowWidth,
    cur_nav: 0,
    scrollLeft: 0,
    nav_list:[],
    cour_list: [],
    show_address: true,
    currentPage: 1,
    pageSize: 10,
    hasMoreDate: true,
    latitude: 22.820794,
    longitude: 113.284565,
    show_nav: false
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that = this;
    var name = options.name;
    wx.setNavigationBarTitle({title: name});
    wx.getStorage({
      key: 'city',
      success:function(res){
        var info=res.data;
        that.setData({latitude:info.lat,longitude:info.lng})
      }
    })
    //
    that.get_cate_list(options.id);
  },
  // 分类切换
  choose_nav:function (e) {
    var that=this,cur_nav=that.data.cur_nav,len=that.data.nav_list.length;
    var id=e.currentTarget.dataset.id,cur=e.currentTarget.dataset.index;
    if(id!=cur_nav){
      that.setData({ cur_nav: id,cour_list:[],currentPage:1,show_nav:false })
      // 数据
      that.get_cour_list(id)
    }
    if(len >= 3){
      var curWidth = (that.data.width - 30) / 5;
      var moveWidth = (cur - 1) * curWidth
      if (moveWidth > 0) {
        that.setData({ scrollLeft: moveWidth })
      } else {
        that.setData({ scrollLeft: 0 })
      }
    }
    // 事件统计
    app.td_app_sdk.event({
      id: 'interestSort',
      label: '兴趣教育小分类',
      params: {
        key1: that.data.nav_list[cur].name
      }
    })
  },
  nav_search:function(){
    wx.navigateTo({
      url: '../search/search',
    })
  },
  // 空数据
  nav_home:function(){
    wx.navigateBack({
      delta: 1
    });
  },
  // 触底
  onReachBottom:function(){
    var that=this,hasMoreData=that.data.hasMoreData,id=that.data.cur_nav;
    if(hasMoreData){
      that.get_cour_list(id)
    }else{
      util.page_show_toast('没有更多了',1500)
    }
  },
  // 获取分类
  get_cate_list:function(id){
    var that=this;
    var params={
      "parentId": id,
      "flagEnable": 1
    }
    home.get_cate_list(params).then(res=>{
      if(res.data.code==200){
        var list=res.data.data,arr=[];
        for(var i=0;i<list.length;i++){
          arr.push(list[i].id)
        }
        var id=arr.join(",")
        that.get_cour_list(id)
        that.setData({nav_list:list,ids:id,cur_nav:id})
      }
    })
  },
  // 获取课程列表
  get_cour_list:function(id){
    wx.showLoading({title: '加载中'})
    var that=this,currentPage=that.data.currentPage,pageSize=that.data.pageSize;
    var params={
      "currentPage": currentPage,
      "pageSize": pageSize,
      "status": 3,
      "categoryIds": id,
      "lat": that.data.latitude,
      "lng": that.data.longitude
    }
    home.get_class_cour_list(params).then(res=>{
      if(res.data.code==200){
        var list=res.data.data.records;
        for(var i=0;i<list.length;i++){
          list[i].introducePictures=list[i].introducePictures?list[i].introducePictures.split(","):'';
          list[i].score=list[i].score?list[i].score.toFixed(1):'5.0';
          list[i].space=list[i].distance>1000?(list[i].distance/1000).toFixed(2):list[i].distance;
          list[i].labelName=list[i].labelName?list[i].labelName.split(","):'';
          list[i].courseNatureName=list[i].courseNatureName?list[i].courseNatureName.substr(0,2):'';
          list[i].sellCount=list[i].sellCount?list[i].sellCount:0
        }
        var cour_list = that.data.cour_list;
        cour_list = util.extend(cour_list, list)
        if (list.length < pageSize) {
          that.setData({
            cour_list: cour_list,
            hasMoreData: false
          })
        } else {
          that.setData({
            cour_list: cour_list,
            hasMoreData: true,
            currentPage: currentPage + 1
          })
        }
        if(list.length==0&&cour_list.length==0){
          that.setData({show_nav:true})
        }
      }
    })
  }
})